#define DEBUG
#include <cstdio>
#include <algorithm>

using namespace std;

typedef long long ll;

const int maxn=1000000, maxsize=20000000, maxq=100000;

void genA(int n, int a[]) {
  ll x;
  int A, B, C, mod;
  scanf("%lld %d %d %d %d", &x, &A, &B, &C, &mod);
  for (int i=1; i<=n; i++) {
    a[i] = x;
    x = (A*x%mod*x%mod+B*x%mod+C)%mod+1;
  }
}

class Que {
public:
  int id;
  ll v;

  static bool cmp(Que &a, Que &b) {
    return a.v<b.v; 
  }
};

void push(int &n, ll a[][2], int x, ll cnt) {
  if (x) {
    if (n && a[n-1][0]==x) {
      a[n-1][1] += cnt;
    } else if (a[n][0]==x) {
      a[n][1] += cnt;
    } else {
      a[++n][0] = x;
      a[n][1] = cnt;
    }
  }
}

bool mycmp(int a, int b) {
  return a>b;
}

int main() {
  freopen("seat.in", "r", stdin);
  freopen("seat.out", "w", stdout);

  static int a[maxn+1], ans[maxq+1];
  static ll b[maxsize+1][2];
  static Que que[maxq+1];
  int n, q;
  scanf("%d", &n);
  genA(n, a);
  sort(a+1, a+n+1, mycmp);
  scanf("%d", &q);
  for (int i=1; i<=q; i++) {
    scanf("%lld", &que[i].v);
    que[i].id = i;
  }
  sort(que+1, que+q+1, Que::cmp);

  int bn=0;
  ll cnt=0;
  for (int i=1, j=1, k=1, last=0; i<=q; i++) {
    for (; cnt<que[i].v; ) {
      if (k>bn || (j<=n && a[j]>=b[k][0])) {
        push(bn, b, a[j]/2, 1);
        push(bn, b, (a[j]-1)/2, 1);
        last = a[j];
        cnt++, j++;
      } else {
        push(bn, b, b[k][0]/2, b[k][1]);
        push(bn, b, (b[k][0]-1)/2, b[k][1]);
        cnt += b[k][1];
        last = b[k][0];
        k++;
      }
    }
    ans[que[i].id] = last;
  }

  for (int i=1; i<=q; i++) {
    printf("%d\n", ans[i]);
  }

  fclose(stdin);
  fclose(stdout);
  return 0;
}
